<template>
  <el-form
    :model="ruleForm2"
    :rules="rules2"
    ref="ruleForm2"
    label-position="left"
    label-width="0px"
    class="demo-ruleForm login-container"
  >
    <h3 class="title">{{$t('Login.Systemlogin')}}</h3>
    <el-form-item prop="account">
      <el-input
        type="text"
        v-model="ruleForm2.account"
        auto-complete="off"
        :placeholder="$t('Login.username')"
      ></el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
      <el-input
        type="password"
        v-model="ruleForm2.checkPass"
        auto-complete="off"
        :placeholder="$t('Login.password')"
      ></el-input>
    </el-form-item>

      <el-form-item 
       label-width="100px"
                :label="$t('Login.language')">
      <el-select style="width:250px"  v-model="languageSelect" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
      </el-select>
    </el-form-item>
    

    <!-- <el-checkbox v-model="checked" checked class="remember"
      >记住密码</el-checkbox 
    >-->
    <el-form-item style="width: 100%">
      <el-button
        type="primary"
        style="width: 100%"
        @click.native.prevent="handleSubmit2"
        :loading="logining"
        @keyup.enter="loginEnter"
        >{{$t('Login.login')}}</el-button
      >
      <!--<el-button @click.native.prevent="handleReset2">重置</el-button>-->
    </el-form-item>
  </el-form>
</template>

<script>
import { postUserRequest } from "../api/api";
import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from '../routes.js'




export default {
  data() {
    return {
      options: [
        {
          value: 'en',
          label: 'English'
        },
        {
          value: 'zh-TW',
          label: '繁體'
        } 
        ],
      languageSelect: sessionStorage.getItem('boslanguage')||'zh-TW',
      logining: false,
      ruleForm2: {
        account: "",
        checkPass: "",
      },
      goolglogin:{
        code:null,
        secret:null
      },
      enterlogin:true,

      // rules2: {
      //   account: [
      //     { required: true, message:this.$t('Login.Systemlogin'), trigger: "blur" },
      //   ],
      //   checkPass: [
      //     { required: true, message: "请输入密码", trigger: "blur" },
      //   ],
      // },
      checked: true,
    };
  },
  watch:{
      languageSelect(newValue,oldValue){
        sessionStorage.setItem("boslanguage", newValue);
        this.$router.go(0);

      }
  },
  computed:{
   rules2:function(){
     return {
        account: [
          { required: true, message:this.$t('Login.username'), trigger: "blur" },
        ],
        checkPass: [
          { required: true, message:this.$t('Login.password'), trigger: "blur" },
        ],
      }
   }
  },
  methods: {
    handleReset2() {
      this.$refs.ruleForm2.resetFields();
    },
    handleSubmit2(ev) {
      var _this = this;
      this.$refs.ruleForm2.validate((valid) => {
        if (valid) {
          this.logining = true;
          var loginParams = {
            username: this.ruleForm2.account,
            pass: this.$md5(this.ruleForm2.checkPass),
          };
          postUserRequest(loginParams, "/loginBos").then((res) => {
             var data = res.data;
             var flag = false
            this.logining = false;
            if (data.code == 200) {
              flag = false

              sessionStorage.setItem("boxToken", data.data.token);
              sessionStorage.setItem("bospower", data.data.roleName);
              Vue.prototype.diction = data.data.roleName
              this.$router.push({ path: "/userstatistics" });

            } else if(data.code == 401) {
              flag = true

              this.enterlogin=false
              this.goolglogin.secret = data.data
              this.$message({
                message: data.message,
                type: "error",
              }); 
    if(flag){
      this.$prompt(this.$t('bulletframe.googletitle'), this.$t('bulletframe.Tips'), {
                confirmButtonText: this.$t('publicbtn.determine'),
                cancelButtonText: this.$t('bulletframe.cancel'),
                inputPattern:  /\S/,
                inputErrorMessage: this.$t('bulletframe.googletitle'),
              }).then(({ value }) => {
                this.goolglogin.code = value
      postUserRequest(this.goolglogin,'/loginByTempKey').then(res=>{
         const data = res.data
         if(data.code == 200){
              sessionStorage.setItem("bingGoogle", data.data.authKey);
              this.enterlogin = true
              sessionStorage.setItem("boxToken", data.data.token);
              sessionStorage.setItem("bospower", data.data.roleName);
              Vue.prototype.diction = data.data.roleName
              this.$router.push({ path: "/userstatistics" });
         }else{
            this.$message({
                message: data.message,
                type: "error",
              });
         }
      })
        }).catch(() => {
          this.$message({
            type: 'info',
            message:this.$t('bulletframe.cancellogin'),
          });       
        });
            }

            }else{
              this.$message({
                message: data.message,
                type: "error",
              }); 
            }
          });
        } else {
          return false;
        }
      });
    },

 
  },
  mounted(){
  },
  created(){
	var _self = this;
	document.onkeydown = function(e){
		if(window.event == undefined){
			var key = e.keyCode;
		}else{
			var key = window.event.keyCode;
		}
		if(key == 13){
      if(_self.enterlogin){
        _self.handleSubmit2();
      }
			
		}
	}
  

  }
};
</script>

<style lang="scss" scoped>
.login-container {
  /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-border-radius: 5px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .remember {
    margin: 0px 0px 35px 0px;
  }
}
</style>